package tienda.zapateria.siszap.da;

import java.sql.Connection;
import tienda.zapateria.siszap.be.BECompra;
import java.util.ArrayList;
import org.rx.cr.dao.DAOAbstract;

public class DACompra extends DAOAbstract<BECompra> {

    public DACompra(Connection c) {
        setConnection(c);
    }

    @Override
    public int registrar(BECompra be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_ins_compra");
            setParameterDate(be.getFecha());
            setParameterInt(be.getProveedorid());
            setParameterString(be.getNumero());

            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int actualizarRegistro(BECompra be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_upd_compra");
            setParameterInt(be.getCompraid());
            setParameterDate(be.getFecha());
            setParameterInt(be.getProveedorid());
             setParameterString(be.getNumero());

            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int eliminarRegistro(BECompra be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_del_compra");
            setParameterInt(be.getCompraid());
            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public ArrayList<BECompra> buscarReferencia(String string) throws Exception {
        try {
            clear();
            setStoreProcedure("zapateria.sp_sel_compra");
            setParameterString(string);
            commitc();
            while (existResult()) {
                BECompra be = new BECompra();
                be.setCompraid(getDataInt(1));
                be.setFecha(getDataDate(2));
                be.setProveedorid(getDataInt(3));
                be.setNumero(getDataString(4));
                
                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public ArrayList<BECompra> listar() throws Exception {
        try {
            clear();
            setStoreProcedure("zapateria.sp_sel_compra");
            setParameterString("");
            commitc();
            while (existResult()) {
                BECompra be = new BECompra();
                be.setCompraid(getDataInt(1));
                be.setFecha(getDataDate(2));
                be.setProveedorid(getDataInt(3));
                be.setNumero(getDataString(4));

                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public BECompra buscar(int i) throws Exception {
        try {
            BECompra be = null;
            clear();
            setStoreProcedure("zapateria.sp_sel_compra_id");
            setParameterInt(i);
            commitc();
            while (existResult()) {
                be = new BECompra();
                be.setCompraid(getDataInt(1));
                be.setFecha(getDataDate(2));
                be.setProveedorid(getDataInt(3));
                be.setNumero(getDataString(4));

            }
            return be;
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    public int obtenerUltimoId() throws Exception {
        BECompra be = new BECompra();
        try {
            setStoreProcedure("sp_obtenercompraid");
            while (existResult()) {
                be.setCompraid(getDataInt(1));
            }

            return Integer.parseInt(commit().toString());

        } catch (Exception e) {
            return rollback(e);
        }
    }
}
